//
//  YJBTradeMarketViewController.m
//  YunJiaBaoFirst
//
//  Created by 腾云小白龍 on 16/7/13.
//  Copyright © 2016年 腾云小白龍. All rights reserved.
//

#import "YJBTradeMarketViewController.h"
#import "YJBTradeMarkSectionView.h"
#import "YJBTradeMarketTableViewCell.h"
#import "YJBTradeMarketFrame.h"
#import "YJBTradeMarketModel.h"
#import "YJBTradeDetailViewController.h"

//#import "YJBTradeTabBarViewController.h"



@interface YJBTradeMarketViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *tradeMarketFrameArray;
@property (nonatomic, assign) BOOL isMJRefresh;

@end

@implementation YJBTradeMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationTitle:@"模拟运力市场"];
    self.tradeMarketFrameArray = [NSMutableArray array];
    _isMJRefresh = NO;
    [self initSubViews];
    [self initData];
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

-(void)initSubViews{
    self.mainTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.mainTableView.delegate   = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.backgroundColor = DEFAULT_TEXT_COLOR;
    self.mainTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.mainTableView];
    
    self.mainTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isMJRefresh = YES;
        [self initData];
    }];
}

#pragma mark -- 加载数据
-(void)initData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    NSString *userToken = [[NSUserDefaults standardUserDefaults] objectForKey:YJBUserToken];
    
    [param setNumber:[NSNumber numberWithInt:11] forKey:@"type"];
    
    NSString *url = [KMainURL stringByAppendingString:getTransportTradeListURL];
    param[@"userToken"] = @"000000000000000b/0b/570c839a128fe1006cadcd09";// userToken;
    
    [[HTAPI shareInstance] YJBActionWithGetRequset:url WithParameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (_isMJRefresh) {
            [self.mainTableView.header endRefreshing];
            [SVProgressHUD showSuccessWithStatus:@"刷新成功"];
            [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
            self.isMJRefresh = NO;
        }
        YJBLog(responseObject)
        [self editTardeMarketDataWithResponseObject:responseObject];
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSString *errorMessage) {
        YJBLog(errorMessage);
        if (_isMJRefresh) {
            [self.mainTableView.header endRefreshing];
            [SVProgressHUD showErrorWithStatus:@"网络超时"];
            [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
            self.isMJRefresh = NO;
        }
    }];
        
}

#pragma mark-- 处理数据
-(void)editTardeMarketDataWithResponseObject:(id)responseObject{
    if (responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.tradeMarketFrameArray removeAllObjects];
            NSMutableArray *array = [NSMutableArray array];
            [array addObjectsFromArray:responseObject];
            for (NSDictionary *dict in array) {
                YJBTradeMarketFrame *frame = [[YJBTradeMarketFrame alloc]init];
                YJBTradeMarketModel *model = [[YJBTradeMarketModel alloc]init];
                model.tradeName = dict[@"name"];
                model.buyPrice = dict[@"buyPrice"];
                model.buyCounts = dict[@"buyQuantity"];
                model.sellCounts = dict[@"sellQuantity"];
                model.id = dict[@"id"];
                model.tradeCode = dict[@"code"];
                model.risePercent = dict[@"riseScale"];
                model.presentCounts = dict[@"quantity"];
                model.presentPrice = dict[@"price"];
                model.sellPrice = dict[@"sellPrice"];
                frame.tradeMarketModel = model;
                [self.tradeMarketFrameArray addObject:frame];

            }
        }

    }
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tradeMarketFrameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YJBTradeMarketTableViewCell *cell = [YJBTradeMarketTableViewCell cellWithTableView:tableView];
    cell.tradeMarketFrame = self.tradeMarketFrameArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YJBTradeMarketFrame *frame = self.tradeMarketFrameArray[indexPath.row];
    
    YJBTradeDetailViewController *vc = [[YJBTradeDetailViewController alloc]init];
    self.tabBarController.tabBar.hidden = YES;
    vc.isBackTradeMarket = YES;
    vc.tradeID = frame.tradeMarketModel.id;
    vc.tradeName = frame.tradeMarketModel.tradeName;
    vc.tradeCode = frame.tradeMarketModel.tradeCode;
    [self.navigationController pushViewController:vc animated:YES];


}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    YJBTradeMarkSectionView *view = [[YJBTradeMarkSectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    return view;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    YJBTradeMarketFrame *frame = [[YJBTradeMarketFrame alloc]init];
//     return  frame.cellHeight;
    return 58;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
